#include "utility.h"

#include <cassert>
#include <cstring>

namespace stdf {

ByteOrder getNativeByteOrder(void) {
    ByteOrder byteOrder = NotDefined;

    unsigned int nativeInt = 0x04030201;

    char* pUINT = (char *) &nativeInt;

    switch(pUINT[0]) {
        case 0x04: byteOrder = BigEndian;
        break;
        case 0x01: byteOrder = LittleEndian;
        break;
        case 0x03: byteOrder = PDPEndian;
        break;
        default:   byteOrder = NotDefined;
    };

    return byteOrder;
}

std::string    time2string(int time) {
    time_t temp = time;
    struct tm *ptm;
    string timeStr;

    ptm = gmtime( &temp);
    string gmtStr = asctime(ptm);
    if(gmtStr[gmtStr.size() - 1] == '\n')
        gmtStr[gmtStr.size() - 1] = ' ';
    timeStr += "  " + gmtStr + " GMT ";

    return timeStr;
}


/// \param[in] pBuf Pointer to char array containing the source data buffer
/// \param[in] size Size of source data buffer in bytes, 1 .. 8
/// \param[in] swap Enable swapping of endianness between Little and Big and vice-versa
void translate( char* pBuf, unsigned int size, bool swap) {
    assert(size >= 2 && size <= 8);
    if(swap) {
        unsigned char p[size];
        //register unsigned char *p = (unsigned char *)pBuf;
        for(unsigned int i=0; i <= (size-1); i++) {
            p[(size-1) - i] = pBuf[i];
        }
        memcpy( pBuf, p, size);
    }
}

/// \param[in] src  Pointer to char array containing the source data buffer
/// \param[in] dest Pointer to char array containing the destination data buffer
/// \param[in] size Size of source data buffer in bytes, 1 .. 8
/// \param[in] swap Enable swapping of endianness between Little and Big and vice-versa
void translate( char* src, char* dest, unsigned int size, bool swap) {
    assert(size >= 2 && size <= 8);
    if(swap) {
        for(unsigned int i=0; i <= (size-1); i++) {
            dest[(size-1) - i] = src[i];
        }
    }
    else {
        for(unsigned int i=0; i <= (size-1); i++)
            dest[i] = src[i];
        //memcpy( src, dest, size);
        }
}


std::string toString(unsigned short& val) {
    stringstream sstr;
    sstr << (int) val;
    return sstr.str();
}
std::string toString(unsigned char& val) {
    stringstream sstr;
    sstr << (int) val;
    return sstr.str();
}
std::string toString(char& val) {
    stringstream sstr;
    sstr << "'" << val << "' " << (int) val;
    return sstr.str();
}
std::string toString(short& val) {
    stringstream sstr;
    sstr << (int) val;
    return sstr.str();
}
std::string toString(int& val) {
    stringstream sstr;
    sstr << val;
    return sstr.str();
}
std::string toString(unsigned int& val) {
    stringstream sstr;
    sstr << val;
    return sstr.str();
}
std::string toString(long int& val) {
    stringstream sstr;
    sstr << val;
    return sstr.str();
}
std::string toString(unsigned long int& val) {
    stringstream sstr;
    sstr << val;
    return sstr.str();
}
std::string toString(float& val) {
    stringstream sstr;
    sstr << val;
    return sstr.str();
}
std::string toString(double& val) {
    stringstream sstr;
    sstr << val;
    return sstr.str();
}
std::string toString(CN& val) {
    stringstream sstr;
    sstr << "[CN " << val.getLength() << "] " << val;
    return sstr.str();
}
std::string toString(BN& val) {
    stringstream sstr;
    sstr << "[BN " << val.getLength() << "] " << val;
    return sstr.str();
}
std::string toString(DN& val) {
    stringstream sstr;
    sstr << "[DN " << val.getLength() << "] " << val;
    return sstr.str();
}

} // end namespace
